package com.axc.gallery2.edit.effective;

import android.graphics.Bitmap;
import android.graphics.Matrix;

import com.axc.gallery2.function.IPicEdit3;

public class ToZoom implements IPicEdit3 {
	float scalewidth;
	float scalheight;

	// 图片的放大缩小
	/**
	 * @author Administrator
	 * @param bitmap
	 * @param w 屏宽或者画布大小
	 * @param h 屏高或者画布大小
	 */
	public Bitmap ProcessImage(Bitmap bitmap, int w, int h) {

		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();

		if (bitmap.getWidth() > w||bitmap.getHeight()>h) {
			if (w < bitmap.getWidth()) {//缩小
				scalewidth = ((float) w / width);
				scalheight = ((float) h / height);
			} else {
				scalewidth = (width / (float) w);
				scalheight = (height / (float) h); 
			}
		} else if (bitmap.getWidth() == bitmap.getHeight()) {
			// 大就放大
			if (w > bitmap.getWidth()) {
				scalewidth = ((float) w / width);
				scalheight = ((float) h / height); 
			}
			// 小则缩小
			else {
				scalewidth = (width / (float) w);
				scalheight = (height / (float) h);
			}

		}
		else { 
			if (w < bitmap.getWidth()) {
				scalewidth = (width / (float) w);
				scalheight = (height / (float) h); 
			} else {
				scalewidth = ((float) w / width);
				scalheight = ((float) h / height); 
			}
			
		}

		matrix.postScale(scalewidth, scalheight);// 对图片进行缩放处理
		// 获取处理后的图片再返回
		Bitmap newbitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return newbitmap;
	}
}
